package Controller;

import Model.ChoreBean;
import Model.UserBean;

import dao.ChoreDAO;
import dao.UserDAO;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author wrjohns
 */
@Named(value = "choreController")
@SessionScoped
public class ChoreController implements Serializable {

    private ChoreBean choreBean;
    private String choreName;
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public ChoreController() {
        choreBean = new ChoreBean();
    }

    public String getChoreName() {
        return choreName;
    }

    public void setChoreName(String choreName) {
        this.choreName = choreName;
    }

    public ChoreBean getChoreBean() {
        return choreBean;
    }

    public void setChoreBean(ChoreBean newBean) {
        choreBean = newBean;
    }

    public String getResponse() {
        if (choreName == null) {
            return "<br><br>";
        }
        String response = "";
        try {
            choreBean.setChoreName(choreName);

            ChoreDAO.init();
            ChoreDAO.loadBean(choreBean);
            ChoreDAO.terminate();
        } catch (SQLException e) {
            return "";
        }
        response += "<h2>" + choreName + "</h2>";
        response += "<p>Points: " + choreBean.getPriority() + "</p>";


        return response;
    }

    public void logChore() {
        try {
            ChoreDAO.init();
            UserBean bean = (UserBean) FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().get("userBean");
            ChoreDAO.logChore(bean,choreBean);
            bean.setPoints(bean.getPoints()+choreBean.getPriority());
            ChoreDAO.terminate();
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Chore Logged"));
        } catch (SQLException ex) {
            Logger.getLogger(ChoreController.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Unable to log chore"));
        }


    }

    public String createChore() {

        choreBean.setStartDate(date);
        try {
            UserBean bean = (UserBean) FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().get("userBean");
            choreBean.setHouseName(bean.getHouseName());
            ChoreDAO.init();
            ChoreDAO.addChore(choreBean);
            ChoreDAO.terminate();
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Chore Created"));
        } catch (SQLException e) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, e);
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Unable to create chore"));
        }
        return "choreManager.xhtml";
    }

    public String deleteChore() {
        try {
            ChoreDAO.init();
            ChoreDAO.deleteChore(choreBean);
            ChoreDAO.terminate();
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Chore Deleted"));
        } catch (SQLException e) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, e);
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Unable to delete chore"));
        }
        return "choreManager.xhtml";
    }

    public String editChore() {
        try {
            ChoreDAO.init();
            ChoreDAO.editChore(choreBean);
            ChoreDAO.terminate();
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Chore Edited"));
        } catch (SQLException ex) {
            Logger.getLogger(ChoreController.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage("chore", new FacesMessage("Unable to Edit"));
        }
        return "choreManager.xhtml";
    }

}